﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;

using Microsoft.Health;
using Microsoft.Health.ItemTypes;
using FusionAPI;

namespace PFSync
{
    /// <summary>
    /// Summary description for SyncItem
    /// </summary>
    public abstract class SyncItem : IComparable<SyncItem>, IComparer<SyncItem>
    {

        public SyncItem()
        {
            //parameterless constructor required for serialize.
        }
        public abstract int ID { get; }

        public abstract HealthRecordItem HVItem { get; }
        public abstract ClinicalDataValue PFItem { get; }

        public abstract int PFTypeId { get; }
        public abstract Guid HVTypeID { get; }

        public virtual object Value { get; set; }
        public virtual MeasurementType Measurement { get; set; }
        public virtual DateTime DateAdded { get; set; }

        /// <summary>
        /// Comparison operator compares to SyncItems to see
        /// if they are equal to each other
        /// </summary>
        /// <param name="item1">SyncItem 1</param>
        /// <param name="item2">SyncItem 2</param>
        /// <returns>True if they are equal</returns>
        public static bool operator ==(SyncItem item1, SyncItem item2)
        {
            // Check Item IDS
            if (item1.ID != item2.ID)
                return false;

            return true;
        }

        /// <summary>
        /// Comparision operator checks to see if two SyncItems
        /// are not equal to each other
        /// </summary>
        /// <param name="item1">SyncItem 1</param>
        /// <param name="item2">SyncItem 2</param>
        /// <returns>True if they are not equal</returns>
        public static bool operator !=(SyncItem item1, SyncItem item2)
        {
            // Check Item IDs
            if (item1.ID == item2.ID)
                return false;

            return true;
        }

        /// <summary>
        /// Override the IComparible CompareTo to allow us
        /// to compare two SyncItems to see if they are the same
        /// </summary>
        /// <param name="obj">SyncItem object</param>
        /// <returns>1 for true</returns>
        public int CompareTo(SyncItem item)
        {
            // See if the item is null
            if (item == null)
                return 1;

            // See if the items are the same
            if (this.ID == item.ID)
                return 0;

            // See 

            return 1;
        }

        /// <summary>
        /// IComparer implementation for sorting abilities
        /// </summary>
        /// <param name="item1">SyncItem 1</param>
        /// <param name="item2">SyncItem 2</param>
        /// <returns>Int for sorting</returns>
        public int Compare(SyncItem item1, SyncItem item2)
        {
            // If item 1 is null, return that item 2 is greater 
            if (item1 == null)
                return -1;

            // If item 2 is null, return that item 1 is greater
            if (item2 == null)
                return 1;

            // If they are equal, return 0
            if (item1.ID == item2.ID)
                return 0;

            // If item 1's date is less than item 2's, return that item 2 is greater
            if (item1.DateAdded < item2.DateAdded)
                return -1;

            // Shouldn't reach this point, but if we do say that they're equal
            return 0;
        }

    }
}
